import {createRouter, createWebHistory, RouteRecordRaw} from "vue-router";
import {localKey} from "../utils/constant";

// 2. 配置路由
const routes: Array<RouteRecordRaw> = [
    {
        path: "/home",
        name: 'home',
        component: () => import("../views/home/Home.vue"),
        meta: {
            auth: true,
        }
    },
    {
        path: "/",
        redirect: '/root/center'
    },
    {
        path: "/root/endcenter",
        redirect: '/root/endcenter/index'
    },
    {
        path: "/root",
        name: 'root',
        component: () => import("../views/root/Root.vue"),
        meta: {
            auth: true,
        },
        children: [
            {
                name: 'center',
                path: 'center',
                component: () => import("../views/ChatCenter/Center/Center.vue"),
                children: [
                    {
                        name: 'chat',
                        path: 'chat',
                        component: () => import("../views/chat/Chat.vue")
                    },
                    {
                        name: 'info',
                        path: 'info',
                        component: () => import("../views/info/Info.vue")
                    },
                    {
                        name: 'show',
                        path: 'show',
                        component: () => import("../views/show/Show.vue")
                    },
                    {
                        name: 'groupInfo',
                        path: 'groupInfo',
                        component: () => import("../views/GroupInfo/GroupInfo.vue")
                    },
                    {
                        name: 'addShow',
                        path: 'addShow',
                        component: () => import("../views/addShow/AddShow.vue")
                    }]
            },
            {
                name: 'endcenter',
                path: 'endcenter',
                component: () => import("../views/ChatCenter/EndCenter/EndCenter.vue"),
                children: [{
                    name: 'userManage',
                    path: 'userManage',
                    component: () => import("../views/EndViews/UserManage/UserManage.vue")
                },{
                    name: 'index',
                    path: 'index',
                    component: () => import("../views/EndViews/Index/Index.vue")
                }]
            }
        ]
    },
    {
        path: "/front",
        name: 'front',
        component: () => import("../views/LoginViews/front/Front.vue"),

        meta: {
            auth: false
        },

        children: [
            {
                path: "login",
                name: 'login',
                component: () => import("../views/LoginViews/login/Login.vue"),
                meta: {
                    auth: false
                }
            },
            {
                path: "registration",
                name: 'registration',
                component: () => import("../views/LoginViews/registration/Registration.vue"),
                meta: {
                    auth: false
                }
            },
            {
                path: 'forget',
                name: 'forget',
                component: () => import("../views/LoginViews/forget/Forget.vue"),
                meta: {
                    auth: false
                }
            }
        ]
    },
];
// 1.返回一个 router 实列，为函数，里面有配置项（对象） history
const router = createRouter({
    history: createWebHistory(),
    routes,
});

//设置路由拦截器
router.beforeEach((to, from, next) => {
    //判断是否需要认证
    if (to.meta.auth) {
        //查看是否有
        if (!localStorage.getItem(localKey.token)) {
            return router.replace({
                name: 'login'
            })
        }
        next()
    } else {
        next()
    }
})

// 3导出路由   然后去 main.ts 注册 router.ts
export default router